package com.xbongbong.paas.help.menu;

import com.xbongbong.paas.pojo.dto.MenuFormAddDTO;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.CallCenterMenuEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuFormAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/8/15 9:58
 * @since v1.0
 */
@Component
public class InitCallCenterMenuHelp {

    private static final Logger LOG = LoggerFactory.getLogger(InitCallCenterMenuHelp.class);

    @Resource
    private PaasMenuService paasMenuService;

    public MenuInitVO initMenu(MenuInitDTO menuInitDTO) throws XbbException {
        MenuInitVO menuInitVO = new MenuInitVO();
        try {
            Long appId = menuInitDTO.getAppId();

            MenuFormAddBatchDTO crmFormAddBatchDTO = new MenuFormAddBatchDTO();
            BeanUtil.copyProperties(menuInitDTO, crmFormAddBatchDTO);
            crmFormAddBatchDTO.setAppId(appId);
            List<MenuFormAddDTO> crmFormMenuList = new ArrayList<>();
            for (CallCenterMenuEnum menuEnum : CallCenterMenuEnum.values()) {

                MenuFormAddDTO menuFormAddDTO = new MenuFormAddDTO();
                BeanUtil.copyProperties(menuInitDTO, menuFormAddDTO);
                menuFormAddDTO.setAppId(appId);
                menuFormAddDTO.setParentId(0L);
                menuFormAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                menuFormAddDTO.setAlias(menuEnum.getAlias());
                menuFormAddDTO.setName(menuEnum.getName());
                AccessLinkEnum accessLinkEnum = menuEnum.getAccessLinkEnum();
                menuFormAddDTO.setIcon(accessLinkEnum.getIcon());
                menuFormAddDTO.setColor(accessLinkEnum.getColor());
                menuFormAddDTO.setUrl(accessLinkEnum.getUrl());
                menuFormAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                menuFormAddDTO.setType(menuEnum.getType());
                crmFormMenuList.add(menuFormAddDTO);
            }
            crmFormAddBatchDTO.setMenuList(crmFormMenuList);
            paasMenuService.addBatchFormMenu(crmFormAddBatchDTO);
        }catch (XbbException e ) {
            LOG.error("InitCallCenterMenuHelp.initMenu 批量添加菜单出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return menuInitVO;
    }
}
